<template>
  <div class="page-container">
    <div class="control-container">
      <div class="right-panel">
        <div class="search-panel">
          <el-form ref="queryForm" :inline="true" label-width="100" :model="query">
            <el-form-item label="关键字">
              <el-input v-model="query.blurry" placeholder="请输入关键字" maxlength="255" />
            </el-form-item>
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button type="primary" @click="handleAddTemplate">新增模板</el-button>
            <el-button @click="clearQuery">重置</el-button>
          </el-form>
        </div>
        <div class="table-panel">
          <el-table
            v-loading="tableLoading"
            border
            size="small"
            highlight-current-row
            :data="tableData"
            style="width: 100%"
          >
            <el-table-column prop="name" label="模板名称" show-overflow-tooltip />
            <el-table-column prop="formType" label="模板类型" show-overflow-tooltip>
              <template slot-scope="scope">{{ getDictLabel('e051', scope.row.formType) }}</template>
            </el-table-column>
            <el-table-column prop="shortName" label="模板简称" show-overflow-tooltip />
            <el-table-column prop="bigType" label="所属大类" show-overflow-tooltip>
              <template slot-scope="scope">{{ getDictLabel('e058', scope.row.bigType) }}</template>
            </el-table-column>
            <el-table-column prop="modelUrl" label="模板地址" show-overflow-tooltip />
            <el-table-column prop="gmtCreate" label="创建日期" show-overflow-tooltip>
              <template slot-scope="scope">
                <div>{{ parseTime(scope.row.gmtCreate, '{y}-{m}-{d}') }}</div>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click.native.stop="handleUpdate(scope.row)">修改</el-button>
                <el-button type="primary" size="mini" @click.native.stop="handleDel(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <AddDialog
      :add-dialog-visible="addDialogVisible"
      :dialog-type="dialogType"
      :obj="obj"
      @closeDialog="addDialogVisible = false"
      @flushPage="flushPage"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
// import { getPage, del } from '@/api/system/roles'
import { parseTime, getDictLabel } from '@/utils/index'
import AddDialog from './components/AddDialog'
import { getPage, del } from '@/api/business/template'

export default {
  name: 'SystemRolePage',
  components: {
    Pagination,
    AddDialog
  },
  data() {
    return {
      obj: null,
      dialogType: 'add',
      addDialogVisible: false,
      tableLoading: false,
      query: {
        page: 1, limit: 10, total: 0, deptId: '',
        blurry: ''
      },
      tableData: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    parseTime, getDictLabel,
    clearQuery() {
      this.query = {
        page: 1, limit: 10, total: 0,
        deptId: '',
        blurry: ''
      }
    },
    initData() {
      const query = JSON.parse(JSON.stringify(this.query))
      this.tableLoading = true
      getPage(query).then(result => {
        this.tableLoading = false
        this.query.total = result.data.list.total
        this.tableData = result.data.list.records
      }).catch(() => {
        this.tableLoading = false
      })
    },
    flushPage() {
      this.clearQuery()
      this.initData()
    },
    handleQuery() {
      this.query.page = 1
      this.initData()
    },
    handleUpdate(row) {
      this.dialogType = 'update'
      this.addDialogVisible = 'true'
      this.obj = row
    },
    handleDel(row) {
      this.$confirm('确认删除此数据?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del([row.id]).then(result => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.handleQuery()
        }).catch(() => {
          this.$message.error('删除失败')
        })
      }).catch(() => {
        this.$message.info('已取消')
      })
    },
    handleAddTemplate() {
      this.dialogType = 'add'
      this.addDialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
